using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;

namespace CallerID
{
    public class Web
    {

        private static string StreamToString(Stream stream, string encoding)
        {
            if(stream != null) {
                StreamReader reader = null;
                if(encoding != null && encoding.Length > 0) {
                    Encoding e = System.Text.Encoding.GetEncoding(encoding);
                    if(e != null) {
                        reader = new StreamReader(stream, e);
                        if(reader != null) return reader.ReadToEnd();
                        return "";
                    }
                }
                reader = new StreamReader(stream, Encoding.ASCII);
                if(reader != null) return reader.ReadToEnd();
            }
            return "";
        }

        private static string StreamToString(Stream stream)
        {
            return StreamToString(stream, "");
        }

        private static byte[] StreamToArray(Stream stream)
        {
            if(stream != null) {
                MemoryStream ms = new MemoryStream();
                int b = stream.ReadByte();
                while(b >= 0 && b <= 255) { ms.WriteByte((byte)b); b = stream.ReadByte(); }
                byte[] array = ms.ToArray();
                ms.Close();
                return array;
            }
            return null;
        }

        private static string GetCookie(CookieCollection cookies)
        {
            if(cookies != null && cookies.Count > 0) {
                string result = "";
                foreach(Cookie cookie in cookies) {
                    if(result.Length > 0) result += "; ";
                    result += cookie.ToString();
                }
                return result;
            }
            return "";
        }

        /// <summary>
        /// Returns WebPage using GET method.
        /// </summary>
        public static WebPage GetWebPage(string url, string cookie, string encoding, int timeout)
        {
            try {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = new CookieContainer();
                if(cookie != null && cookie.Length > 0) request.CookieContainer.SetCookies(request.RequestUri, cookie);
                if(timeout > 0) request.Timeout = Math.Min(Math.Max(timeout, 3000), 30000);
                if(request != null) {
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    if(response != null) {
                        string html = StreamToString(response.GetResponseStream(), (encoding != null && encoding.Length > 0 ? encoding : response.CharacterSet));
                        response.Close();
                        return new WebPage(html, GetCookie(response.Cookies));
                    }
                }
            } catch { }
            return null;
        }

        /// <summary>
        /// Returns WebPage using POST method.
        /// </summary>
        public static WebPage GetWebPage(string url, string postvars, string cookie, string encoding, int timeout)
        {
            try {
                byte[] buffer = Encoding.ASCII.GetBytes(postvars);
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = new CookieContainer();
                if(cookie != null && cookie.Length > 0)  request.CookieContainer.SetCookies(request.RequestUri, cookie);
                if(request != null) {
                    request.Method = "POST";
                    if(timeout > 0) request.Timeout = Math.Min(Math.Max(timeout, 3000), 30000);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = buffer.Length;
                    Stream post = request.GetRequestStream();
                    if(post != null) {
                        post.Write(buffer, 0, buffer.Length);
                        post.Close();
                        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                        if(response != null) {
                            string html = StreamToString(response.GetResponseStream(), (encoding != null && encoding.Length > 0 ? encoding : response.CharacterSet));
                            response.Close();
                            return new WebPage(html, GetCookie(response.Cookies));
                        }
                    }
                }
            } catch { }
            return null;
        }

        public static byte[] GeWebImage(string url)
        {
            try {
                WebRequest request = HttpWebRequest.Create(url);
                if(request != null) {
                    WebResponse response = request.GetResponse();
                    if(response != null) {
                        byte[] img = StreamToArray(response.GetResponseStream());
                        response.Close();
                        return img;
                    }
                }
            } catch { }
            return null;
        }
    }

    public class WebPage
    {
        private string m_Html;
        private string m_Cookie;

        public string Html
        {
            get { return m_Html ?? ""; }
        }
        public string Cookie
        {
            get { return m_Cookie ?? ""; }
        }
        public WebPage(string html, string cookie)
        {
            m_Html = html;
            m_Cookie = cookie;
        }
    }
}
